package com.bjb.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


/**
 * 抽象过滤器
 * 用于过滤掉不需拦截的请求及供其他过滤器继承
 *
 */
public abstract class AbstractFilter implements Filter  {

	private FilterConfig config;

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;

		//----------------------------------------以后添加放行URL集合
		try {
			doFilter(request, response, chain);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	/**
	 * 抽象方法
	 * @param request
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 * @throws SQLException
	 */
	protected abstract void doFilter(HttpServletRequest request,HttpServletResponse response,FilterChain chain) throws IOException, ServletException, SQLException;

	/**
	 * 初始化
	 * @param config
	 */
	protected abstract void doInit(FilterConfig config);

	@Override
	public void init(FilterConfig config) throws ServletException {

		this.config = config;
		doInit(config);
	}

	public FilterConfig getConfig() {
		return config;
	}

	@Override
	public void destroy() {}

}
